﻿using JGSY.DotNetty.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.DotNetty.Server.CoreImpl
{
  
    public static class MimeManager
    {
        private static readonly Dictionary<string, string> _mimeDic;
        static MimeManager()
        {
            string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MIMEConfig.json");
            if (!File.Exists(configFilePath)) throw new DotNettyServerException("MIMEConfig.json文件丢失");
            string jsonConfigString = File.ReadAllText(configFilePath);
            _mimeDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonConfigString);
        }
        /// <summary>
        /// 获得ContentType
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static string GetContentType(string extension)
        {
            return _mimeDic.ContainsKey(extension) ? _mimeDic[extension] : "application/octet-stream";
        }
    }
}

//        private static readonly Dictionary<string, string> mimeDic = new Dictionary<string, string>();
//        static MimeManager()
//        {
//            mimeDic.Add(".html", "text/html");
//            mimeDic.Add(".js", "application/javascript");
//            mimeDic.Add(".css", "text/css");
//        }
//        /// <summary>
//        /// 获得ContentType
//        /// </summary>
//        /// <param name="extension"></param>
//        /// <returns></returns>
//        public static string GetContentType(string extension)
//        {
//            return mimeDic.ContainsKey(extension) ? mimeDic[extension] : "application/octet-stream";
//        }
//    }
//}
